<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'device:management:inEquipmentType:editButton'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsDeviceManagementInEquipmentTypeForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { InEquipmentType, inEquipmentTypeSave, inEquipmentTypeForm } from '/@/api/device.management/inEquipmentType';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('device.management.inEquipmentType');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<InEquipmentType>({} as InEquipmentType);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增') : t('编辑'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('设备类型名称'),
      field: 'equipmentName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
      required: true,
    },
    {
      label: t('设备类型编码'),
      field: 'equipmentCode',
      
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
      required: true,
    },
    {
      label: t('是否联动设备'),
      field: 'islinked',
      fieldLabel: 'islinked',
      component: 'Select',
      componentProps: {
        maxlength: 10,
        options: [
          {label: t('是'), value: '01'},
          {label: t('否'), value: '10'},
        ]
      },
    },
    {
      label: t('维保周期 '),
      field: 'cycle',
      fieldLabel: 'cycle',
      component: 'Select',
      componentProps: {
        maxlength: 20,
        options: [
          {label: t('年'), value: '01'},
          {label: t('季'), value:'10'},
          {label: t('月'), value: '20'},
        ]
      },
    },
    {
      label: t('维保方式 '),
      field: 'maintenanceMethodCode',
      fieldLabel: 'maintenanceMethod',
      component: 'Select',
      componentProps: {
        maxlength: 20,
        dictType: '	maintenance_method',
        fieldNames:{lable:"lable",value:"value"},
        allowClear:true
      },
    },
    {
      label: t('监督检查设备百分比'),
      field: 'inspectPercent',
      component: 'InputNumber',
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
      componentProps: {
        min: 0,
        max: 100,
        step: 1,
        addonAfter: '%'
      }
    },
    {
      label: t('监测设备百分比'),
      field: 'jcPercent',
      component: 'InputNumber',
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
      componentProps: {
        min: 0,
        max: 100,
        step: 1,
        addonAfter: '%'
      }
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await inEquipmentTypeForm(data);
    record.value = (res.inEquipmentType || {}) as InEquipmentType;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };
      // console.log('submit', params, data, record);
      const res = await inEquipmentTypeSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
